<?php
/*
 * FileName:class_http.php
 */

class Http {
	static function getWebContent($host, $method = 'GET', $header = '') {
		//connect server
		$fp = fsockopen($host, 80, $errno, $errstr, 10);
		if (!$fp) {
			echo ("can't open this server " . $host);
			return false;
		}

		fputs($fp, "$method\r\n");
		fputs($fp, "Host: $host\r\n");

		//send header
		if (!empty ($header)) {
			fputs($fp, $header);
		}
		fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n\r\n");

		//get web content
		$response = '';
		while (!feof($fp)) {
			$response .= fgets($fp, 1024);
		}
		$hlen = strpos($response, "\r\n\r\n"); //Under LINUX is "\n\n"
		$header = substr($response, 0, $hlen);
		$entity = substr($response, $hlen +4);

		//output stream
		$a['all'] = $response;
		$a['header'] = $header;
		$a['content'] = $entity;
		fclose($fp);
		//return
		return $a;
	}

	/**
	 * 取得客户端ip地址
	 * @return string $ip
	 */
	function getIP() {
		if (getenv( 'HTTP_CLIENT_IP' )) {
			$ip = getenv( 'HTTP_CLIENT_IP' );
		}
		elseif (getenv( 'HTTP_X_FORWARDED_FOR' )) {
			$ip = getenv( 'HTTP_X_FORWARDED_FOR' );
		}
		elseif (getenv( 'HTTP_X_FORWARDED' )) {
			$ip = getenv( 'HTTP_X_FORWARDED' );
		}
		elseif (getenv( 'HTTP_FORWARDED_FOR' )) {
			$ip = getenv( 'HTTP_FORWARDED_FOR' );
		}
		elseif (getenv( 'HTTP_FORWARDED' )) {
			$ip = getenv( 'HTTP_FORWARDED' );
		}
		else {
			$ip = $_SERVER[ 'REMOTE_ADDR' ];
		}
		return $ip;
	}//end function

	/**
	 * 处理Url
	 *
	 * @param string $url
	 * @return array 合法的URL数组
	 */
	function getUrl($url){
		$urls = parse_url( $url );
		if( empty( $urls['host'] ) ){
			$urls['host'] = $_SERVER["SERVER_NAME"];
		}
		if( empty( $urls['scheme'] ) ){
			$urls['scheme'] = 'http';
		}
		if( empty( $urls['port'] )){
			$urls['port'] = $_SERVER['SERVER_PORT'];
		}
		if( empty( $urls['path'] )){
			$urls['path'] = $_SERVER['PHP_SELF'];
		}
		return $urls;
	}

	// escape for URL
	function escapeForURL ($input) {
		return str_replace("&", "&amp;", $input);
	}

	function meta($charset='utf-8'){
		echo '<meta http-equiv="Content-Type" content="text/html; charset='.$charset.'" />';
	}

	/**
	 * 根据用户IP,利用dig去查询相应的PTR记录
	 * @param string $ip
	 * @return string $domain
	 */
	function getPTR($ip){
		$output = shell_exec("/usr/bin/dig -x {$ip}");
		$arr=explode("\n",$output);//分行存入数组

		$ptrstr="";
		$pos=false;//读取ptr记录的开关

		foreach($arr as $line){
			if($pos){
				$ptrstr=$line;
				break;
			}
			//检查到answer，启动读取开关
			if(strstr($line,';; ANSWER SECTION:')) $pos=true;
		}

		//命令行返回的ptr记录值
		//$ptrstr="82.60.208.203.in-addr.arpa. 84927 IN	PTR	crawl-203-208-60-82.googlebot.com."
		if(empty($ptrstr)) return false;

		//域名字符串
		//$ptrStrArr[2]="crawl-203-208-60-82.googlebot.com."
		$ptrStrArr=explode("\t", $ptrstr);
		if(empty($ptrStrArr[2]))  return false;

		//小于3的域名忽略不计
		$ptrDomainArr=explode('.', $ptrStrArr[2]);
		$n=count($ptrDomainArr);
		if($n<3) return false;

		//返回类似 googlebot.com
		$domain=$ptrDomainArr[$n-3].'.'.$ptrDomainArr[$n-2];
		return $domain;
	}
}

?>